package com.paas.form.service.impl;

import com.paas.common.jdbc.ResultHandler;
import com.paas.common.jdbc.model.DbColumn;
import com.paas.common.util.BeanUtil;
import com.paas.common.util.StringUtil;
import com.paas.form.entity.FormMasterApplyEntity;
import com.paas.form.entity.FormViewPubEntity;
import com.paas.plus.tool.realTimeDistributed.model.RealTimeDistributeConfig;
import com.paas.plus.tool.realTimeDistributed.model.RealTimeDistributeExecutionParameter;
import com.paas.plus.tool.realTimeDistributed.server.IRealTimeDistributeServer;
import lombok.Data;

import java.util.*;


@Data

public class ApplyHandler implements ResultHandler {


    private FormMasterApplyEntity applyEntity;


    private FormViewPubEntity view;

    private IRealTimeDistributeServer realTimeDistributeServer;


    private RealTimeDistributeConfig realTimeDistributeConfig;



    @Override
    public void handleResult(Map<String, Object> data) {
        String idField = view.getIdField();
        String id = null;
        RealTimeDistributeExecutionParameter realTimeDistributeExecutionParameter = new RealTimeDistributeExecutionParameter();

        HashMap<String, Object> tempMap = new HashMap<>();
        tempMap= (HashMap<String, Object>) data;
        ArrayList<HashMap<String, Object>> arrayList = new ArrayList<>();
        arrayList.add(tempMap);
        realTimeDistributeExecutionParameter.setListMap(arrayList);

        //判断当前参数中,是否包含视图id
        if (tempMap.containsKey(idField.toUpperCase()) || tempMap.containsKey(idField.toLowerCase())){
            if (BeanUtil.isNotEmpty(tempMap.get(idField.toUpperCase()))){
                id = tempMap.get(idField.toUpperCase()).toString();
            }else {
                id = tempMap.get(idField.toLowerCase()).toString();
            }

            realTimeDistributeExecutionParameter.setPrimaryKeyField(idField.toUpperCase());
            realTimeDistributeExecutionParameter.setType("insert");
            if(StringUtil.isNotEmpty(applyEntity.getDataType())){
                realTimeDistributeExecutionParameter.setType(applyEntity.getDataType());
            }


            realTimeDistributeServer.executeDistribute(realTimeDistributeConfig,realTimeDistributeExecutionParameter);
        }




    }

    @Override
    public void setColumns(List<DbColumn> dbColumnList) {

    }




}
